/*
 * CollectFragment.java
 * business
 *
 * Created by SunDing on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.collect;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.yidiandao.business.R;
import com.yidiandao.business.base.BaseFragment;
import com.yidiandao.business.ui.adapter.CollectAdapter;
import com.yidiandao.business.ui.collect.goods.CollectGoodsFragment;
import com.yidiandao.business.ui.collect.goods.CollectGoodsPresenter;
import com.yidiandao.business.ui.collect.shop.CollectShopFragment;
import com.yidiandao.business.ui.collect.shop.CollectShopPresenter;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

/** 我的收藏. */
public class CollectFragment extends BaseFragment implements CollectContract.View {

    private static final String TAG = CollectFragment.class.getSimpleName();

    private CollectContract.Presenter mPresenter;
    private Toolbar mToolbar;
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private CollectAdapter mCollectAdapter;
    private CollectGoodsFragment mCollectGoodsFragment;
    private CollectShopFragment mCollectShopFragment;

    public static CollectFragment getInstance() {
        return new CollectFragment();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (null != savedInstanceState) {
            mCollectAdapter = new CollectAdapter(getChildFragmentManager());
            List<Fragment> fragments = getChildFragmentManager().getFragments();
            for (Fragment fragment : fragments) {
                if (fragment instanceof CollectGoodsFragment) {
                    mCollectGoodsFragment = (CollectGoodsFragment) fragment;
                } else if (fragment instanceof CollectShopFragment) {
                    mCollectShopFragment = (CollectShopFragment) fragment;
                }
            }
            addFragments();
        }
    }

    @Override
    protected void initViews() {
        mToolbar = (Toolbar) mRootView.findViewById(R.id.toolbar);
        mTabLayout = (TabLayout) mRootView.findViewById(R.id.tab_layout);
        mViewPager = (ViewPager) mRootView.findViewById(R.id.view_pager);

        initViewPager();

        initEvent();
    }

    private void initViewPager() {
        if (mCollectAdapter == null) {
            mCollectAdapter = new CollectAdapter(getChildFragmentManager());
            mCollectGoodsFragment = CollectGoodsFragment.getInstance();
            mCollectShopFragment = CollectShopFragment.getInstance();
            addFragments();
        }
        new CollectGoodsPresenter(mCollectGoodsFragment);
        new CollectShopPresenter(mCollectShopFragment);

        mViewPager.setAdapter(mCollectAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
    }

    private void addFragments() {
        mCollectAdapter.addFragment(mCollectGoodsFragment);
        mCollectAdapter.addFragment(mCollectShopFragment);
    }

    private void initEvent() {
        mToolbar.setNavigationOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mActivity.finish();
                    }
                });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_collect;
    }

    @Override
    public void setPresenter(CollectContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return TAG;
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }
}
